package study.springcloud.gateway.service.auth;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ServerWebExchange;
import study.springcloud.gateway.support.utils.WebUtils;

import java.util.Map;
import java.util.function.Consumer;

@Slf4j
@Service("E2mf")
public class E2mfAuthService implements AuthService {

    private static final String NAME_E2MF = "e2mf";

    @Override
    public boolean doAuth(ServerWebExchange exchange, ServerHttpRequest request) {
        String e2mf = WebUtils.getCookieValue(exchange, NAME_E2MF);
        if (StrUtil.isEmpty(e2mf)) {
            log.warn("[E2mf Auth] fail: e2mf empty");
            return false;
        }
        log.info("[E2mf Auth] e2mf: {}", e2mf);
        //
        Map<String, String> headers = Maps.newHashMap();
        headers.put("G-RequestId", "123");
        headers.put("G-Name", "123");

        WebUtils.addRequestHeaders(exchange, headers);
        return true;
    }

    public static Consumer<HttpHeaders> buildHeader(String key, String value) {
        Consumer<HttpHeaders> consumer = headers -> {
            headers.set(key, value);
        };
        return consumer;
    }
}
